#include "OMInterfaceDX10.h"
#include "../Texture/Texture2DDX10.h"

COMInterfaceDX10::COMInterfaceDX10( )
{
    m_uRenderTargetsCount = 0;
    m_bUseDepthStencil    = false;
    m_ppRTV               = NULL;
    m_pOrigRTV            = NULL;
    m_pOrigDSV            = NULL;
    m_ViewPort.MinDepth   = 0.0f;
    m_ViewPort.MaxDepth   = 0.0f;
    m_ViewPort.TopLeftX   = 0;
    m_ViewPort.TopLeftY   = 0;
}

COMInterfaceDX10::~COMInterfaceDX10( )
{
    for (mUInt i = 0; i < m_uRenderTargetsCount; i++)
    {
        SAFE_RELEASE(m_ppRTV[i]);
    }
    SAFE_RELEASE(m_pOrigRTV);
    SAFE_RELEASE(m_pOrigDSV);
    if (m_bUseDepthStencil)
        SAFE_RELEASE(m_pDSV);
    delete [] m_ppRTV;
}

mVoid COMInterfaceDX10::CreateRenderTarget( CTexture2D *a_pTexture[], mUInt a_uRenderTargetsCount )
{
    if (m_uRenderTargetsCount)
        return;
    m_uRenderTargetsCount = a_uRenderTargetsCount;
    m_ViewPort.Width      = a_pTexture[0]->Width();
    m_ViewPort.Height     = a_pTexture[0]->Height();
    m_ppRTV = new ID3D10RenderTargetView*[m_uRenderTargetsCount];
    D3D10_RENDER_TARGET_VIEW_DESC RTVDesc;
    ZeroMemory( &RTVDesc, sizeof(RTVDesc) );
    RTVDesc.ViewDimension = D3D10_RTV_DIMENSION_TEXTURE2D;
    RTVDesc.Texture2D.MipSlice = 0;
    CTexture2DDX10 *pTexDX10;
    for (mUInt i = 0; i < m_uRenderTargetsCount; i++)
    {
        RTVDesc.Format = DX10ImageFmt(a_pTexture[i]->ImageFormat());
        pTexDX10 = static_cast< CTexture2DDX10* >(a_pTexture[i]);
        g_D3DDevice.pD3D10Device->CreateRenderTargetView(pTexDX10->Texture(), 
            &RTVDesc, &m_ppRTV[i]);
    }
}

mVoid COMInterfaceDX10::CreateDepthStencil( CTexture2D *a_pTexture )
{
    CTexture2DDX10 *pTexDX10 = static_cast< CTexture2DDX10* >(a_pTexture);
    if (m_ViewPort.Width == 0)
    {
        m_ViewPort.Width      = a_pTexture->Width();
        m_ViewPort.Height     = a_pTexture->Height();
    }
    else 
    {
        if (m_ViewPort.Width != a_pTexture->Width() || m_ViewPort.Height != a_pTexture->Height())
            return;//some shit must happens, mmm        
    }
    D3D10_DEPTH_STENCIL_VIEW_DESC DSVDesc;
    ZeroMemory( &DSVDesc, sizeof(DSVDesc) );
    DSVDesc.Format             = DXGI_FORMAT_D32_FLOAT;
    DSVDesc.ViewDimension      = D3D10_DSV_DIMENSION_TEXTURE2D;
    DSVDesc.Texture2D.MipSlice = 0;
    g_D3DDevice.pD3D10Device->CreateDepthStencilView(pTexDX10->Texture(), &DSVDesc, &m_pDSV);

    m_bUseDepthStencil = true;
}

mVoid COMInterfaceDX10::SaveCurrentRT( )
{
    UINT uNumVP = 1;
    g_D3DDevice.pD3D10Device->RSGetViewports(&uNumVP, &m_PrevViewPort);
    g_D3DDevice.pD3D10Device->OMGetRenderTargets(1, &m_pOrigRTV, &m_pOrigDSV);
}

mVoid COMInterfaceDX10::RestoreRT( )
{
    g_D3DDevice.pD3D10Device->RSSetViewports(1, &m_PrevViewPort);
    g_D3DDevice.pD3D10Device->OMSetRenderTargets(1, &m_pOrigRTV, m_pOrigDSV);
}

mVoid COMInterfaceDX10::SetRenderTargets( )
{
    g_D3DDevice.pD3D10Device->RSSetViewports(1, &m_ViewPort);
    if (m_bUseDepthStencil)
        g_D3DDevice.pD3D10Device->OMSetRenderTargets(m_uRenderTargetsCount, m_ppRTV, m_pDSV);
    else
        g_D3DDevice.pD3D10Device->OMSetRenderTargets(m_uRenderTargetsCount, m_ppRTV, m_pOrigDSV);
}



